<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_model extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
		$this->load->helper('cookie');
		$this->load->helper('date');
		$this->load->library('session');
    }
	
	function increase_create_score($user_id, $amount)
	{
		return $this->_increase_score($user_id, 'create', $amount);
	}
	
	function increase_vote_score($user_id, $amount)
	{
		return $this->_increase_score($user_id, 'vote', $amount);
	}
	
	function _increase_score($user_id, $type, $amount)
	{
		$sql = "SELECT score_{$type} as score FROM user WHERE id = '{$user_id}'";
		$query = $this->db->query($sql);
		if($query->num_rows() == 0)
			return FALSE;
		
		$res = $query->row();
		$new_score = $res->score + $amount;
		
		$sql = "UPDATE user SET score_{$type}='{$new_score}' WHERE id = '$user_id'";
		$query = $this->db->query($sql);
		if(mysql_affected_rows() > 0)
			return $new_score;
		else
			return FALSE;
	}
	
	function register($data)
	{		
		if($data && $data['username'] && $data['password'])
		{
			$data['password'] = $this->hash_password($data['username'], $data['password']);
			
			$keys = implode("`,`", array_keys($data));
			$values = implode("','", array_values($data));
			
			$sql = "INSERT INTO user(`$keys`) VALUE ('$values');";
			$query = $this->db->query($sql);
			return $query;
		}
		return FALSE;
	}
	
	function get_user_by_id($id)
	{
		$sql = "SELECT * FROM `user` WHERE `id`='$id'";
		$query = $this->db->query($sql);
		if($query->num_rows() == 1)
			return $query->row();
		return FALSE;
	}
	
	function get_users_by_ids($arr)
	{
		if(empty($arr))
			return array();
		
		foreach($arr as $k => $i)
		{
			$arr[$k] = "(id = '$i')";
		}
		$where = implode(' OR ', $arr);
		
		$sql = "SELECT id, username, name FROM user WHERE $where";
		$query = $this->db->query($sql);
		if($query->num_rows() == 1)
			return $query->result();
		return array();
	}
	
	function get_user_by_username($username)
	{
		$sql = "SELECT * FROM `user` WHERE `username`='$username'";
		$query = $this->db->query($sql);
		if($query->num_rows() == 1)
			return $query->row();
		return FALSE;
	}
	
	function get_all_users()
	{
		$sql = "SELECT * FROM `user`";
		$query = $this->db->query($sql);
		if($query->num_rows() > 0)
			return $query->result();
		return NULL;
	}
	
	function check_username($username)
	{
		$sql = "SELECT `id` FROM `user` WHERE `username`='$username'";
		$query = $this->db->query($sql);
		return $query->num_rows() == 0;
	}
	
	function login($username, $password)
	{
		$sql = "SELECT `id`, `username`, `password`, `is_admin` FROM `user` WHERE `username`='$username'";
		$query = $this->db->query($sql);
		$user = $query->row();
		
		if($query->num_rows() == 1)
		{
			$password = $this->hash_password($username, $password);
			
			if($user->password === $password)
			{
				$session_data = array(
					'user_id'	=> $user->id,
					'username'	=> $user->username,
					'is_admin'	=> $user->is_admin
				);
				
				$this->session->set_userdata($session_data);
				return TRUE;
			}
		}
		return FALSE;
	}
	
	function hash_password($username, $password)
	{
		return sha1($username.$password);
	}
}